package com.gitee.srurl.core.factory.handle;

import cn.hutool.core.collection.CollectionUtil;
import com.gitee.srurl.core.annotation.Order;
import com.gitee.srurl.core.api.IDisabledStrategy;
import com.gitee.srurl.core.config.ShortUrlConfig;
import com.gitee.srurl.core.dto.request.GenerateShortUrlDTO;
import com.gitee.srurl.core.factory.ShortUrlFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zxw
 * @date 2023/4/29 8:17
 */
@Component
@Order(Integer.MIN_VALUE)
public class DisabledStrategyHandle implements IGenerateShortUrlHandle {
    @Override
    public boolean handle(GenerateShortUrlDTO generateShortUrlDTO) {

        ShortUrlConfig shortUrlConfig = ShortUrlFactory.getShortUrlConfig();
        List<IDisabledStrategy> disabledStrategy = shortUrlConfig.getDisabledStrategy();
        if (CollectionUtil.isEmpty(disabledStrategy)) {
            return true;
        }

        String originUrl = generateShortUrlDTO.getOriginUrl();
        for (IDisabledStrategy iDisabledStrategy : disabledStrategy) {
            boolean disabled = iDisabledStrategy.disabled(originUrl);
            if (disabled) {
                generateShortUrlDTO.setMsg("该url禁止生成短链");
                generateShortUrlDTO.setSuccess(false);
                return false;
            }
        }
        generateShortUrlDTO.setSuccess(true);
        return true;
    }
}
